﻿Imports System.Data.SqlClient
Public Class EditProfile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Here we can Only change our profile
        If Not IsPostBack Then
            Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("BooksReviewDBConnectionString1").ConnectionString.ToString)
            Dim cmd As New SqlCommand("SELECT * FROM Profiles WHERE Bibliophile = @f1", conn)
            conn.Open()
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)

            'Getting records (only 1 line for selected Bibliophile)
            Dim dr As SqlDataReader = cmd.ExecuteReader()
            If dr.HasRows Then
                dr.Read()
                If Not IsDBNull(dr("FirstName")) Then tb_FirstName.Text = dr("FirstName")
                If Not IsDBNull(dr("LastName")) Then tb_LastName.Text = dr("LastName")
                If Not IsDBNull(dr("Description")) Then tb_Description.Text = dr("Description")
                If Not IsDBNull(dr("Location")) Then tb_Location.Text = dr("Location")
                If Not IsDBNull(dr("DateOfBirth")) Then tb_DateOfBirth_CalendarExtender.SelectedDate = dr("DateOfBirth")
            End If
            dr.Close()
            Dim cmdLanguage As New SqlCommand("SELECT * FROM Profile_CodingLanguage WHERE Bibliophile = @f1", conn)
            cmdLanguage.CommandType = CommandType.Text
            cmdLanguage.Parameters.AddWithValue("@f1", User.Identity.Name)
            'Add a tick if user has selected a Language.
            Dim drLanguage As SqlDataReader = cmdLanguage.ExecuteReader()
            If drLanguage.HasRows Then
                cbl_Languages.DataBind()
                While drLanguage.Read()
                    For Each cd As ListItem In cbl_Languages.Items
                        If cd.Value = drLanguage("CodingLanguage_FK").ToString Then
                            cd.Selected = True
                        End If
                    Next
                End While
            End If
            drLanguage.Close()

            'Count Activity???????????????????

        End If
        If IsPostBack Then
            tb_DateOfBirth_CalendarExtender.SelectedDate = DateTime.ParseExact(tb_DateOfBirth.Text, tb_DateOfBirth_CalendarExtender.Format, Nothing)
        End If
    End Sub

    Protected Sub btn_SaveProfile_Click(sender As Object, e As EventArgs) Handles btn_SaveProfile.Click

        Dim newFileName As String = ""
        Dim SqlString As String

        'Check if the file upload is not empty
        If (f_Picture.HasFile) Then
            Dim myGUID As New System.Guid
            myGUID = Guid.NewGuid()
            Dim ext As String = IO.Path.GetExtension(f_Picture.FileName)
            newFileName = myGUID.ToString() + ext
            'Check if picture uploaded is a valid picture.
            If isPicture(ext) Then
                Dim picLocationOnHardDiskServer = Request.MapPath("Pictures/ProfilePictures") & "/" & newFileName
                f_Picture.SaveAs(picLocationOnHardDiskServer)
            Else
                Response.Redirect("EditProfile.aspx")
            End If
        End If
        Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("BooksReviewDBConnectionString1").ConnectionString.ToString)

        'If member has added a picture
        If (f_Picture.HasFile) Then
            SqlString = "UPDATE Profiles SET FirstName = @f1, LastName = @f2, Description = @f3, BibliophileEmail = @f4, Picture = @f5, DateOfBirth = @f6, Location = @f7 WHERE Bibliophile = @f8"
        Else
            SqlString = "UPDATE Profiles SET FirstName = @f1, LastName = @f2, Description = @f3, BibliophileEmail = @f4, DateOfBirth = @f6, Location = @f7 WHERE Bibliophile = @f8"
        End If
        Dim cmd As SqlCommand = New SqlCommand(SqlString, conn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", tb_FirstName.Text)
        cmd.Parameters.AddWithValue("@f2", tb_LastName.Text)
        cmd.Parameters.AddWithValue("@f3", tb_Description.Text)
        cmd.Parameters.AddWithValue("@f4", Membership.GetUser(User.Identity.Name).Email.ToString)
        If (f_Picture.HasFile) Then
            cmd.Parameters.AddWithValue("@f5", "Pictures/ProfilePictures/" & newFileName)
        End If
        cmd.Parameters.AddWithValue("@f6", tb_DateOfBirth_CalendarExtender.SelectedDate)
        cmd.Parameters.AddWithValue("@f7", tb_Location.Text)
        cmd.Parameters.AddWithValue("@f8", User.Identity.Name)

        conn.Open()
        cmd.ExecuteNonQuery()
        'Delete entries to enter new selections from user
        Dim deleteSql As String = "DELETE FROM Profile_CodingLanguage WHERE Bibliophile= @f1"
        Dim cmdDelete As SqlCommand = New SqlCommand(deleteSql, conn)
        cmdDelete.CommandType = CommandType.Text
        cmdDelete.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmdDelete.ExecuteNonQuery()
        For Each cd As ListItem In cbl_Languages.Items
            If cd.Selected Then
                Dim languageSql As String = "INSERT INTO Profile_CodingLanguage (Bibliophile, CodingLanguage_FK) VALUES (@f1, @f2)"
                Dim cmdLanguage As SqlCommand = New SqlCommand(languageSql, conn)
                cmdLanguage.CommandType = CommandType.Text
                cmdLanguage.Parameters.AddWithValue("@f1", User.Identity.Name)
                cmdLanguage.Parameters.AddWithValue("@f2", cd.Value)
                cmdLanguage.ExecuteNonQuery()
            End If
        Next
        'Eliminate cmd and oledbconn from memory
        cmd.Dispose()
        conn.Close()
        conn.Dispose()

        Response.Redirect("Profile.aspx?Bibliophile=" & User.Identity.Name)
    End Sub

    'Check if picture uploaded is a valid picture.
    Private Function isPicture(ByVal pictureExtension As String) As Boolean
        Select Case pictureExtension
            Case ".jpg"
                Return True
            Case ".JPG"
                Return True
            Case ".jpeg"
                Return True
            Case ".JPEG"
                Return True
            Case ".png"
                Return True
            Case ".PNG"
                Return True
            Case ".bmp"
                Return True
            Case ".BMP"
                Return True
            Case ".gif"
                Return True
            Case ".GIF"
                Return True
            Case Else
                Return False
        End Select
    End Function

End Class